/* Copyright (C) 2004 Stefan Bellon <sbellon@sbellon.de>
 *
 * This file is part of JetDirectFS.
 *
 * JetDirectFS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JetDirectFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <kernel.h>

#include <oslib/osfscontrol.h>

#include "config.h"
#include "interface.h"
#include "statics.h"

#ifdef DEBUG
FILE *logfile;
#endif

static void
jetdirectfs_finalise(void)
{
#ifdef DEBUG
    fclose(logfile);
#endif
    
    xosfscontrol_remove_fs(FilingSystemName);
}

static os_error *
declare_jetdirectfs(void *pw)
{
    osfscontrol_fs_info_block info_block = {
        (int)FilingSystemName         - (int)Image_RO_Base, // name_offset
        (int)FilingSystemName         - (int)Image_RO_Base, // banner_offset
        (int)&veneer_fsentry_open     - (int)Image_RO_Base, // open_offset
        (int)&veneer_fsentry_getbytes - (int)Image_RO_Base, // get_bytes
        (int)&veneer_fsentry_putbytes - (int)Image_RO_Base, // put_bytes
        (int)&veneer_fsentry_args     - (int)Image_RO_Base, // args_offset
        (int)&veneer_fsentry_close    - (int)Image_RO_Base, // close_offset
        (int)&veneer_fsentry_file     - (int)Image_RO_Base, // file_offset
        FilingSystemNumber,                                 // info
        (int)&veneer_fsentry_func     - (int)Image_RO_Base, // func_offset
        (int)&veneer_fsentry_gbpb     - (int)Image_RO_Base, // gbpb_offset
        0                                                   // extra_info
    };

    return xosfscontrol_add_fs
      (Image_RO_Base, (int)&info_block - (int)Image_RO_Base, pw);
}

os_error *
jetdirectfs_initialise(char *cmd_tail, int podule_base, void *pw)
{
    os_error *err;

    UNUSED(cmd_tail);
    UNUSED(podule_base);

#ifdef DEBUG
    logfile = fopen("$.log", "w");
#endif

    err = declare_jetdirectfs(pw);

    if (!err)
        atexit(jetdirectfs_finalise);

    return err;
}

void jetdirectfs_service(int service_number, _kernel_swi_regs *r, void *pw)
{
    UNUSED(service_number);
    UNUSED(r);

    declare_jetdirectfs(pw);
}
